﻿using System;
using System.Data;
using MySql.Data.MySqlClient;
using KPIS.DBM;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Text = "select * from sys_sc_users";
                TextBox2.Text = "server=10.8.99.71;user id=root;pwd=root123456789;database=gerp";
            }
        }

        private DataTable GetDataTable()
        {
            DataTable dtb = new DataTable();
            string sql = TextBox1.Text;
            string connectionString = TextBox2.Text;

            if (DropDownList1.SelectedValue == "2")
            {
                MySqlConnection conn = new MySqlConnection(connectionString);
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand(sql, conn);
                adapter.Fill(dtb);
            }
            else
            {
                DBManager dbManager = new DBManager(DataProvider.SqlServer, connectionString);
                try
                {
                    dbManager.Open();
                    dtb = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    dbManager.Dispose();
                }
            }

            return dtb;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = GetDataTable();
            GridView1.DataBind();
        }
    }
}
